<?php
/**
 * CakeCMS - 公告管理
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
App::import ( 'Vendor', 'Uploader' );
class NoticeController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'AdminLog' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->Uploader = new Uploader ();
		$this->Uploader->setup ( array (
				'tempDir' => CAKE_CORE_INCLUDE_PATH . DS . 'www' . DS . 'tmp' . DS,
				'baseDir' => CAKE_CORE_INCLUDE_PATH . DS . 'www' . DS . 'webroot' . DS,
				'uploadDir' => 'files' . DS . 'notice' . DS . date ( "Ymd", time () ) 
		) );
		
		$this->set ( 'title_for_layout', 'CakeCMS公告管理' );
	}
	/**
	 * 公告列表
	 */
	public function index() {
		$this->set ( 'title_for_layout', 'CakeCMS公告管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'notice.js' 
		) );
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Notice" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array ();
		
		$list = $this->Notice->GetNoticeByList ( $conditions, $limit );
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 公告分类
	 */
	public function cate() {
		$this->set ( 'title_for_layout', 'CakeCMS公告分类管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'notice.js' 
		) );
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "NoticeCate" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array ();
		
		$list = $this->NoticeCate->GetNoticeCateByList ( $conditions, $limit );
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 添加分类
	 */
	public function addCate() {
		if ($this->request->isPost ()) {
			$data ['NoticeCate'] ['name'] = $this->data ['name'];
			if (empty ( $data ['NoticeCate'] ['name'] )) {
				$this->flash ( '分类名称不能为空!', '/Notice/addCate/', 1250 );
				return false;
			}
			$this->loadModel ( "NoticeCate" );
			if ($this->NoticeCate->add ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '添加公告分类!' );
				
				$this->flash ( '添加分类成功!', '/Notice/cate/', 1250 );
				return false;
			} else {
				$this->flash ( '添加分类失败!', '/Notice/addCate/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', 'CakeCMS添加分类' );
		}
	}
	/**
	 * 编辑分类
	 */
	public function editCate() {
		if ($this->request->isPost ()) {
			$data ['NoticeCate'] ['name'] = $this->data ['name'];
			if (empty ( $data ['NoticeCate'] ['name'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '分类名称不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			
			$data ['NoticeCate'] ['id'] = $this->data ['id'];
			if (empty ( $data ['NoticeCate'] ['id'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '参数错误!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			
			$this->loadModel ( "NoticeCate" );
			if ($this->NoticeCate->edit ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '修改公告分类,ID:' . $this->data ['id'] );
				
				$msg = array (
						'rtn_code' => 0,
						'content' => '编辑分类成功!' 
				);
				echo json_encode ( $msg );
				exit ();
			} else {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '编辑分类失败!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
		} else {
			$this->set ( 'title_for_layout', 'CakeCMS编辑分类' );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'notice.js' 
			) );
			
			$id = $this->params ['id'];
			
			$this->loadModel ( "NoticeCate" );
			$CateInfo = $this->NoticeCate->GetNoticeCateByID ( $id );
			$this->set ( 'CateInfo', $CateInfo );
		}
	}
	/**
	 * 删除分类
	 */
	public function deleteCate() {
		$id = $this->data ['id'];
		
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "NoticeCate" );
		if ($this->NoticeCate->del ( $id )) {
			
			// 记录操作日志
			$this->AdminLog->addLog ( '删除公告分类,ID:' . $id );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
	/**
	 * 添加公告
	 */
	public function add() {
		if ($this->request->isPost ()) {
			$data ['Notice'] ['cateid'] = $this->data ['cateid'];
			if (empty ( $data ['Notice'] ['cateid'] )) {
				$this->flash ( '公告分类不能为空!', '/Notice/', 1250 );
				return false;
			}
			$data ['Notice'] ['title'] = $this->data ['title'];
			if (empty ( $data ['Notice'] ['title'] )) {
				$this->flash ( '公告标题不能为空!', '/Notice/', 1250 );
				return false;
			}
			$data ['Notice'] ['content'] = $this->data ['content'];
			if (empty ( $data ['Notice'] ['content'] )) {
				$this->flash ( '公告内容不能为空!', '/Notice/', 1250 );
				return false;
			}
			$data ['Notice'] ['starttime'] = $this->data ['starttime'];
			if (empty ( $data ['Notice'] ['starttime'] )) {
				$this->flash ( '起始日期不能为空!', '/Notice/', 1250 );
				return false;
			}
			$data ['Notice'] ['endtime'] = $this->data ['endtime'];
			if (empty ( $data ['Notice'] ['endtime'] )) {
				$this->flash ( '截止日期不能为空!', '/Notice/', 1250 );
				return false;
			}
			$data ['Notice'] ['username'] = $this->Session->read ( 'username' );
			if (empty ( $data ['Notice'] ['username'] )) {
				$this->flash ( '发布人不能为空!', '/Notice/', 1250 );
				return false;
			}
			$data ['Notice'] ['hits'] = 0;
			$data ['Notice'] ['state'] = $this->data ['state'];
			if (! isset ( $data ['Notice'] ['state'] )) {
				$this->flash ( '公告状态不能为空!', '/Notice/', 1250 );
				return false;
			}
			$this->loadModel ( "Notice" );
			if ($this->Notice->add ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '添加公告!' );
				
				$this->flash ( '添加公告成功!', '/Notice/', 1250 );
				return false;
			} else {
				$this->flash ( '添加公告失败!', '/Notice/add/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', 'CakeCMS添加公告' );
			
			// 加载css文件
			$this->set ( 'cssList', array (
					'calendar/jscal2.css',
					'calendar/border-radius.css',
					'calendar/win2k.css',
					'kindeditor/themes/default/default.css',
					'kindeditor/plugins/code/prettify.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'calendar/calendar.js',
					'calendar/lang/en.js',
					'jquery.cookie.js',
					'colorpicker.js',
					'kindeditor/kindeditor.js',
					'kindeditor/lang/zh_CN.js',
					'kindeditor/plugins/code/prettify.js',
					'content.js' 
			) );
			
			$this->loadModel ( "NoticeCate" );
			$conditions = array ();
			$result = $this->NoticeCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			$this->set ( 'Catelist', $result );
		}
	}
	/**
	 * 编辑公告
	 */
	public function edit() {
		if ($this->request->isPost ()) {
			$data ['Notice'] ['cateid'] = $this->data ['cateid'];
			if (empty ( $data ['Notice'] ['cateid'] )) {
				$this->flash ( '公告分类不能为空!', "/Notice/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Notice'] ['title'] = $this->data ['title'];
			if (empty ( $data ['Notice'] ['title'] )) {
				$this->flash ( '公告标题不能为空!', "/Notice/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Notice'] ['content'] = $this->data ['content'];
			if (empty ( $data ['Notice'] ['content'] )) {
				$this->flash ( '公告内容不能为空!', "/Notice/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Notice'] ['starttime'] = $this->data ['starttime'];
			if (empty ( $data ['Notice'] ['starttime'] )) {
				$this->flash ( '起始日期不能为空!', "/Notice/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Notice'] ['endtime'] = $this->data ['endtime'];
			if (empty ( $data ['Notice'] ['endtime'] )) {
				$this->flash ( '截止日期不能为空!', "/Notice/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Notice'] ['username'] = $this->Session->read ( 'username' );
			if (empty ( $data ['Notice'] ['username'] )) {
				$this->flash ( '发布人不能为空!', "/Notice/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Notice'] ['state'] = $this->data ['state'];
			if (! isset ( $data ['Notice'] ['state'] )) {
				$this->flash ( '公告状态不能为空!', "/Notice/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			
			$data ['Notice'] ['id'] = $this->data ['id'];
			
			$this->loadModel ( "Notice" );
			if ($this->Notice->edit ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '编辑公告,ID:' . $this->data ['id'] );
				
				$this->flash ( '编辑公告成功!', '/Notice/', 1250 );
				return false;
			} else {
				$this->flash ( '编辑公告失败!', "/Notice/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', 'CakeCMS编辑公告' );
			
			// 加载css文件
			$this->set ( 'cssList', array (
					'calendar/jscal2.css',
					'calendar/border-radius.css',
					'calendar/win2k.css',
					'kindeditor/themes/default/default.css',
					'kindeditor/plugins/code/prettify.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'calendar/calendar.js',
					'calendar/lang/en.js',
					'jquery.cookie.js',
					'colorpicker.js',
					'kindeditor/kindeditor.js',
					'kindeditor/lang/zh_CN.js',
					'kindeditor/plugins/code/prettify.js',
					'content.js' 
			) );
			
			$this->loadModel ( "NoticeCate" );
			$conditions = array ();
			$result = $this->NoticeCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			$this->set ( 'Catelist', $result );
			
			$id = $this->params ['id'];
			
			$this->loadModel ( "Notice" );
			$NoticeInfo = $this->Notice->GetNoticeByID ( $id );
			$this->set ( 'NoticeInfo', $NoticeInfo );
		}
	}
	/**
	 * 删除公告
	 */
	public function delete() {
		$id = $this->data ['id'];
		
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "Notice" );
		if ($this->Notice->del ( $id )) {
			
			// 记录操作日志
			$this->AdminLog->addLog ( '删除公告,ID:' . $id );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
}
?>